package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsDevBindInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsDevBindInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangbin on 2020/3/31
 */
public class CopsDevBindInfoMapperImpl implements CopsDevBindInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCPS_POOL_NAME);


    @Override
    public List<CopsDevBindInfo> getDevBindInfoByMchntNo(String instId, String bindDevType, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsDevBindInfoByMN");
        CopsDevBindInfo devBindInfo = new CopsDevBindInfo();
        devBindInfo.setInstId(instId);
        devBindInfo.setBindMchntNo(mchntNo);
        devBindInfo.setBindDevType(bindDevType);
        devBindInfo.setBindStatus("00");
        devBindInfo.setRecordStatus("01");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, devBindInfo, CopsDevBindInfo.class);
        if (objectList.isEmpty()) {
            rglog.error("获取设备绑定信息失败");
            return null;
        }
        return objectList.stream().map(item -> (CopsDevBindInfo) (item)).collect(Collectors.toList());

    }
}
